export default function dataReducers(state = {
    user: [
        { key: 1, id: 1, name: '金秋9月优惠月', content: '满200减20，满300-50', type: '满减券', Claimed: 791, remainder: 209, state: '进行中', item: ['2023-01-06 18:45', '2023-01-08 18:45'] },
        { key: 2, id: 2, name: '国庆节福利发放', content: '满500，打6折', type: '折扣券', Claimed: 791, remainder: 209, state: '未开始', item: ['2022-12-06 18:45', '2022-12-08 18:45'] },
        { key: 3, id: 3, name: '中秋钜惠', content: '满100-50', type: '满减券', Claimed: 791, remainder: 209, state: '已结束', item: ['2022-12-31 18:45', '2023-01-01 18:45'] }
    ],
    arr: [
        {
            key: 1, id: 1, name: '金秋9月秒杀活动', time: '2022-10-19 9:39:39---2022-11-19 9:39:39', quantity: 966, state: '进行中', zt: '上线中', commodity: [
                { key: 0, name: '吸尘器', price: '1288.00', stock: 966, sales: 1999 },
            ],
            moment: ['16:00']
        },
        {
            key: 2, id: 2, name: '国庆秒杀', time: '2022-10-01 9:39:39---2022-10-07 9:39:39', quantity: 1256, state: '未开始', zt: '已下线', commodity: [
                { key: 2, name: '榨汁机', price: '299.00', stock: 1256, sales: 72 },
            ],
            moment: ['12:30']
        },
        {
            key: 3, id: 3, name: '中秋钜惠秒杀', time: '2022-09-15 9:39:39---2022-10-01 9:39:39', quantity: 789, state: '进行中', zt: '上线中', commodity: [
                { key: 5, name: '电磁炉', price: '265.00', stock: 789, sales: 265 },
            ],
            moment: ['8:00']
        }
    ],
    list: [
        { key: 0, name: '吸尘器', price: '1288.00', stock: 966, sales: 1999 },
        { key: 1, name: '电饭煲', price: '588.00', stock: 855, sales: 680 },
        { key: 2, name: '榨汁机', price: '299.00', stock: 1256, sales: 72 },
        { key: 3, name: '面包机', price: '300.00', stock: 341, sales: 345 },
        { key: 4, name: '微波炉', price: '750.00', stock: 586, sales: 786 },
        { key: 5, name: '电磁炉', price: '265.00', stock: 789, sales: 265 },
    ],
    inst: [],
}, action) {
    switch (action.type) {
        case 'ADDUSER':
            {
                let user = JSON.parse(JSON.stringify(state.user))
                action.payload.id = state.user.length + 1
                action.payload.key = state.user.length + 1
                user.push(action.payload)
                return { ...state, user };
            }
        case 'ADDARR':
            {
                let arr = JSON.parse(JSON.stringify(state.arr))
                action.payload.key = state.arr.length + 1
                action.payload.id = state.arr.length + 1
                arr.push(action.payload)
                return { ...state, arr }
            }
        case 'ADDINST':
            {
                let a = JSON.parse(JSON.stringify(state))
                a.inst = action.payload
                return { ...a };
            }
        case 'DELETEINST':
            {
                let inst = JSON.parse(JSON.stringify(state.inst))
                let a = action.payload
                inst.forEach((el, index) => {
                    if (el.key === a.key) {
                        inst.splice(index, 1)
                    }
                })
                return { ...state, inst }
            }
        case 'EDITARR':
            {
                let arr = JSON.parse(JSON.stringify(state.arr))
                arr.forEach((el, index) => {
                    if (el.key === action.payload.key) {
                        arr.splice(index, 1, action.payload)
                    }
                })
                return { ...state, arr }
            }
        case 'EDITUSER':
            {
                let user = JSON.parse(JSON.stringify(state.user))
                user.forEach((el, index) => {
                    if (el.key === action.payload.key) {
                        user.splice(index, 1, action.payload)
                    }
                })
                return { ...state, user }
            }
        default:
            return state;
    }
}